import lazyLoading from "./lazyLoading.js"
import router from "./index.js"
import api from "../api/api.js"
import store from './../store'

let routerUtil = {

};

routerUtil.initRouter = function(vm,update){
	api.sysMenuFindUserMenuByTree().then(result=>{
		let menuList = result
		//添加所有菜单
		store.commit('updateMenulist', menuList);
		//配置路由
		const otherRoutes = []
		routerUtil.initRouterNode(otherRoutes,menuList)
		console.info("初始化路由",otherRoutes)
		otherRoutes.forEach((item)=>{
			router.addRoute("home",{
			    name:item.name,
			    path:item.path,
			    component: item.component
			})
		})
	})
}

// 生成路由节点
routerUtil.initRouterNode = function (otherRoutes, data) {

    for (var item of data) {
        let menu = {
			name:item.name,
			path:item.routerUrl,
		};
		if(item.type == 'ZICAIDAN'){
			menu.component = lazyLoading(item.component)
			otherRoutes.push(menu);
		}

        if (item.children && item.children.length > 0) {
            menu.children = [];
            routerUtil.initRouterNode(otherRoutes, item.children);
        }
    }
};

export default routerUtil;